﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Xml;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using System.Collections.Specialized;

namespace WorkerRole.Main
{
    class Settings
    {


        #region XML
        public static string GetInputXML(LogDataContext logContext)
        {
            try
            {
                CloudStorageAccount storageAccount;
                CloudBlobClient blobClient;

                storageAccount = CloudStorageAccount.FromConfigurationSetting("InputXMLBlobConnectionString");

                blobClient = storageAccount.CreateCloudBlobClient();
                blobClient.RetryPolicy = RetryPolicies.Retry(4, TimeSpan.Zero);

                CloudBlobContainer blobContainer = blobClient.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("InputXMLBlobContainerName"));
                CloudBlob blob = blobContainer.GetBlobReference("SQLAzureBackupData.xml");

                return blob.DownloadText();
            }
            catch (Exception Ex)
            {
                logContext.SaveError("Getting Input XML cause issue. The error: " + Ex.Message + " at " + DateTime.Now.ToString("ddMMMMyyyy HHMMSS"));
            }
            return string.Empty;
        }

        public static void PutInputXML(LogDataContext logContext,string xmlData)
        {
            try
            {
                CloudStorageAccount storageAccount;
                CloudBlobClient blobClient;

                storageAccount = CloudStorageAccount.FromConfigurationSetting("InputXMLBlobConnectionString");

                blobClient = storageAccount.CreateCloudBlobClient();
                blobClient.RetryPolicy = RetryPolicies.Retry(4, TimeSpan.Zero);

                CloudBlobContainer blobContainer = blobClient.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("InputXMLBlobContainerName"));
                CloudBlob blob = blobContainer.GetBlobReference("SQLAzureBackupData.xml");

                blob.UploadText(xmlData);
            }
            catch (Exception Ex)
            {
                logContext.SaveError("Getting Input XML cause issue. The error: " + Ex.Message + " at " + DateTime.Now.ToString("ddMMMMyyyy HHMMSS"));
            }
        }
        #endregion


    }
}
